/*
 *   Time Tracking Web Application
 *   Copyright (C) 2008, Nick Griffiths
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   Contact nicobrevin at gmail dot com
 *   http://code.google.com/p/openharvest/
 */

package org.brevs.time.model;

import java.util.Date;
import java.util.Timer;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.appfuse.model.BaseObject;
import org.appfuse.model.User;

/**
 * The big daddy, the Entry object.  Records time for a {@link User} against a 
 * {@link Project} and a {@link ProjectCategory}.
 * @author nick
 *
 */
@Entity
@Table(name="entry")
public class Entry extends BaseObject {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@Column(nullable=false)
	private long seconds;
	
	@Column(length=1024, nullable=true)
	private String description;
	
	@Column(name="start_date", nullable=false)
	private Date startDate;
	
	@Column(name="started_running", nullable=true)
	private Date startedRunning;
	
	@ManyToOne()
	@JoinColumn(name="user")
	private User user;
	
	@ManyToOne
	@JoinColumn(name="project")
	private Project project;
	
	@ManyToOne
	@JoinColumn(name="project_category")
	private ProjectCategory projectCategory;
	
	/**
	 * The primary key.
	 * @return
	 */
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	/**
	 * A user defined description for an entry, i.e. what they were doing.
	 * @return
	 */
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	/**
	 * How many seconds have been recorded against this entry.  Won't include
	 * any time since the timer was last started - see {@link #getStartedRunning()}
	 * @return
	 */
	public long getSeconds() {
		return seconds;
	}
	public void setSeconds(long seconds) {
		this.seconds = seconds;
	}
	
	/**
	 * The date to which the entry belongs.  
	 * @return
	 */
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	/**
	 * The time on which this {@link Entry} object's timer started running.
	 * Will be <code>null</code> if the entry isn't running.
	 * @return
	 */
	public Date getStartedRunning() {
		return startedRunning;
	}
	public void setStartedRunning(Date startedRunning) {
		this.startedRunning = startedRunning;
	}
	
	/**
	 * Is the timer running.  
	 * @return
	 */
	public boolean isRunning() {
		return startedRunning != null;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	/**
	 * Returns the number of millis that this entry has been running for, or 0
	 * if it isn't running.
	 * @return
	 */
	public long getRunningFor() {
		return isRunning() ? new Date().getTime() - getStartedRunning().getTime() : 0;
	}
	
	/**
	 * The user who owns this {@link Entry}
	 * @return
	 */
	public User getUser() {
		return user;
	}
	
	/**
	 * The {@link Project} which time on this entry is logged against.
	 * @return
	 */
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	
	/**
	 * The {@link ProjectCategory} which time on this entry is logged against.
	 * @return
	 */
	public ProjectCategory getProjectCategory() {
		return projectCategory;
	}
	public void setProjectCategory(ProjectCategory projectCategory) {
		this.projectCategory = projectCategory;
	}
	@Override
	public boolean equals(Object o) {
		Entry rhs = (Entry) o;
		return new EqualsBuilder()
			.append(this.seconds, rhs.seconds)
			.append(this.description, rhs.description)
			.append(this.project, rhs.project)
			.append(this.user, rhs.user)
			.isEquals();
	}
	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(this.seconds)
			.append(this.description)
			.append(this.project)
			.append(this.user)
			.hashCode();
	}
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
}
